<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>基础的ajax请求</h1>
  <form id="form" action="" method="">
    <input id="ipt" type="text">
    <button id="btn">点击获取学生的信息</button>
  </form>
  <script>
    // 获取元素
    const Oipt = document.querySelector('#ipt')
    const Otn = document.querySelector('#btn')
    const Oform = document.querySelector('#form')

    // 给表单绑定事件
    Oform.onsubmit = () => {
      // 1.创建xhr实例
      const xhr = new XMLHttpRequest()

      // 2.打开xhr
      xhr.open('GET', '/user', true)

      // 3.发送
      xhr.send()

      // 4.判断
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status <= 299) {
          console.log(xhr.responseText)
        }
      }
      return false
    }
  </script>
</body>

</html>